#include "modifydialog.h"
#include "ui_modifydialog.h"

ModifyDialog::ModifyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

}

ModifyDialog::~ModifyDialog()
{
    delete ui;
}

void ModifyDialog::SetQuery(QSqlQuery &q)
{
    this->query = q;
}
void ModifyDialog::SetWindowTitle(QString windowTitle)
{
    this->windowtitle = windowTitle;
    this->setWindowTitle(windowTitle+"修改");
}
void ModifyDialog::AddItem()
{
      QStringList list;
      QString text = windowtitle.mid(0,2);
      query.exec(QString("select  column_name,column_type from information_schema.columns where table_name='%1'").arg(text));
      while(query.next())
      {
          list<<query.value(0).toString();
      }
      this->field = list;
      ui->comboBox->addItems(list);
      query.exec(QString("select  * from %1").arg(text));
      AddConnect();
      ui->dateTimeEdit->setVisible(false);
      ui->lineEdit->setVisible(false);

}
void ModifyDialog::Push()
{
    QString text = ui->lineEdit->text();
    QString seetName = this->windowtitle.mid(0,2);
    QString attribute;
    if(seetName=="用户")
    {
        attribute = QString("update %1 set User_name='%2',Phone_num='%3',Car_char='%4' where %5='%6'").arg(seetName).arg(ui->lineEdit->text()).arg(ui->lineEdit_2->text()).arg(ui->lineEdit_3->text()).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText());

    }
    else if(seetName=="预定")
    {
        attribute = QString("update %1 set Pre_outtime='%2',Pre_intime='%3',Phone_num='%4', Set_id='%5' where %6='%7'").arg(seetName).arg(ui->lineEdit->text()).arg(ui->lineEdit_2->text()).arg(ui->lineEdit_3->text()).arg(ui->lineEdit_4->text()).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText());

    }
    else if(seetName=="停车")
    {
        attribute = QString("update %1 set Set_id='%2',Pre_time='%3',In_time='%4', Car_char='%5',Phone_num='%6' where %7='%8'").arg(seetName).arg(ui->lineEdit->text()).arg(ui->lineEdit_2->text()).arg(ui->lineEdit_3->text()).arg(ui->lineEdit_4->text()).arg(ui->lineEdit_5->text()).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText());
    }
    else if(seetName=="开出")
    {
        attribute = QString("udate %1 set Set_id='%2',Out_time='%3',Out_time='%4',Car_char='%5' where %6='%7'").arg(seetName).arg(ui->lineEdit->text()).arg(ui->lineEdit_2->text()).arg(ui->lineEdit_3->text()).arg(ui->lineEdit_4->text()).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText());
    }
    else
    {
        attribute = QString("update %1 set Have_car='%2',Distance='%3',Price='%4',War_price='%5' where %6='%7'").arg(seetName).arg(ui->lineEdit->text()).arg(ui->lineEdit_2->text()).arg(ui->lineEdit_3->text().split("￥")[0]).arg(ui->lineEdit_4->text().split("￥")[0]).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText());
    }
    qDebug()<<attribute;
    query.prepare(attribute);
    bool ok = query.exec();
    if(!ok)
    {
        QSqlError error = query.lastError();
        QMessageBox::warning(this,"Warning",tr(error.text().toUtf8().data()),QMessageBox::Ok);
    }
    else
    {
        QMessageBox::information(this,"Finish",tr("修改成功"),QMessageBox::Ok);
    }
}
void ModifyDialog::AddConnect()
{

    connect(ui->comboBox,&QComboBox::currentTextChanged,ui->comboBox_2,[=](const QString& text)
    {
        QStringList list;
        ui->comboBox_2->clear();
        if(this->windowtitle=="车位表")
        {
            query.exec("select * from 车位");
            if(text=="Distance")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Have_car")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Price")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
        }
        else if(this->windowtitle=="停车表")
        {
            query.exec("select * from 停车");

            if(text=="Stop_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Pre_time")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="In_time")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Car_char")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(5).toString();
                }
            }
        }
        else if(this->windowtitle=="开出表")
        {
            query.exec("select * from 开出");

            if(text=="Out_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Out_name")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Out_time")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Car_char")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
        }
        else if(this->windowtitle=="用户表")
        {
            query.exec("select * from 用户");
            if(text=="User_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="User_name")
            {

                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }

        }
        else if(this->windowtitle=="预定表")
        {
            ui->comboBox_2->clear();
            query.exec("select * from 预定");
            if(text=="Pre_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Pre_outtime")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Pre_intime")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
            ui->comboBox_2->addItems(list);
        }
        ui->comboBox_2->addItems(list);
        query.first();
        query.previous();

    });
    connect(ui->comboBox_2,&QComboBox::currentTextChanged,ui->lineEdit,[=](const QString& text){
        SetModel();
        ui->lineEdit->setVisible(true);
        ui->lineEdit->setPlaceholderText(text);


    });
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        this->Push();
    });
    connect(ui->treeView,&QTreeView::pressed,[=](const QModelIndex& index){
        try {
                if(index.parent().isValid())
                {
                    int indexLineEdit = index.row();
                    QString text = index.data().toString().split("：")[1];
                    putInLineEdit(indexLineEdit,text);
                }


        } catch (...) {

        }

    });
}
void ModifyDialog::SetModel()
{
    ui->lineEdit_2->setVisible(false);
    ui->lineEdit_3->setVisible(false);
    ui->lineEdit_4->setVisible(false);
    ui->lineEdit_5->setVisible(false);
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_3->clear();
    ui->lineEdit_4->clear();
    ui->lineEdit_5->clear();
    this->model = new QStandardItemModel();
    QList<QStandardItem*> list;
    QString attribute = QString("select * from %3 where %1='%2'").arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText()).arg(this->windowtitle.mid(0,2));
    if(ui->comboBox_2->currentText()=="")
    {
       attribute = QString("select * from %3 where %1 is null").arg(ui->comboBox->currentText()).arg(this->windowtitle.mid(0,2));
    }

    query.exec(attribute);
    if(windowtitle=="用户表")
    {
        while(query.next())
        {
            QStandardItem* item = new QStandardItem("用户ID：" + query.value(0).toString());
            QList<QStandardItem*> chilist;
            chilist.push_back(new QStandardItem("用户名：" + query.value(1).toString()));
            chilist.push_back(new QStandardItem("电话：" + query.value(2).toString()));
            chilist.push_back(new QStandardItem("车牌：" + query.value(3).toString()));

            item->appendRows(chilist);
            list.push_back(item);

        }
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->lineEdit_3->setVisible(true);
    }
    else if(windowtitle=="开出表")
    {
        while(query.next())
        {
            QStandardItem* item = new QStandardItem("开出ID：" + query.value(0).toString());
            QList<QStandardItem*> chilist;
            chilist.push_back(new QStandardItem("车位号：" + query.value(1).toString()));
            chilist.push_back(new QStandardItem("开出人员：" + query.value(2).toString()));
            chilist.push_back(new QStandardItem("开出时间：" + query.value(3).toString()));
            chilist.push_back(new QStandardItem("车牌：" + query.value(4).toString()));

            item->appendRows(chilist);
            list.push_back(item);

        }
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->lineEdit_3->setVisible(true);
        ui->lineEdit_4->setVisible(true);
    }
    else if(windowtitle=="预定表")
    {
        while(query.next())
        {
            QStandardItem* item = new QStandardItem("预定ID：" + query.value(0).toString());
            QList<QStandardItem*> chilist;
            chilist.push_back(new QStandardItem("预定开出时间：" + query.value(1).toString()));
            chilist.push_back(new QStandardItem("预定开入时间：" + query.value(2).toString()));
            chilist.push_back(new QStandardItem("预留电话：" + query.value(3).toString()));
            chilist.push_back(new QStandardItem("车位：" + query.value(4).toString()));

            item->appendRows(chilist);
            list.push_back(item);
        }
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->lineEdit_3->setVisible(true);
        ui->lineEdit_4->setVisible(true);
    }
    else if(windowtitle=="停车表")
    {
        while(query.next())
        {
            QStandardItem* item = new QStandardItem("停车ID：" + query.value(0).toString());
            QList<QStandardItem*> chilist;
            chilist.push_back(new QStandardItem("车位号：" + query.value(1).toString()));
            chilist.push_back(new QStandardItem("规定开入时间：" + query.value(2).toString()));
            chilist.push_back(new QStandardItem("开入时间：" + query.value(3).toString()));
            chilist.push_back(new QStandardItem("车牌：" + query.value(4).toString()));
            chilist.push_back(new QStandardItem("电话：" + query.value(5).toString()));

            item->appendRows(chilist);
            list.push_back(item);
        }
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->lineEdit_3->setVisible(true);
        ui->lineEdit_4->setVisible(true);
        ui->lineEdit_5->setVisible(true);
    }
    else
    {
        while(query.next())
         {
            QStandardItem* item = new QStandardItem("车位ID：" + query.value(0).toString());
            QList<QStandardItem*> chilist;
            chilist.push_back(new QStandardItem("有车？：" + query.value(1).toString()));
            chilist.push_back(new QStandardItem("离出口距离：" + query.value(2).toString()));
            chilist.push_back(new QStandardItem("价格：" + query.value(3).toString() + "￥/h"));
            chilist.push_back(new QStandardItem("违规价格：" + query.value(4).toString() + "￥/h"));

            item->appendRows(chilist);
            list.push_back(item);
         }
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->lineEdit_3->setVisible(true);
        ui->lineEdit_4->setVisible(true);
    }
    this->model->appendColumn(list);
    ui->treeView->setModel(this->model);
}
void ModifyDialog::putInLineEdit(const int &row,const QString& text)
{
    if(row==0)
    {
        ui->lineEdit->setText(text);
    }
    else if(row==1)
    {
        ui->lineEdit_2->setText(text);
    }
    else if(row==2)
    {
        ui->lineEdit_3->setText(text);
    }
    else if(row==3)
    {
        ui->lineEdit_4->setText(text);
    }
    else
    {
        ui->lineEdit_5->setText(text);
    }
}
